<?php
/**
 * FieldCompare.php
 * 
 * @category    Madoqua
 * @package     Validate
 */

/** base class */
require_once 'Zend/Validate/Abstract.php';

/**
 * Madoqua_Validate_FieldCompare
 *
 * @category    Madoqua
 * @package     Validate
 */
class Madoqua_Validate_FieldCompare extends Zend_Validate_Abstract
{
    /**
     * ahw, no match
     *
     * @var string
     */
    const NOT_MATCH = 'notMatch';

    /**
     * array of fields to match
     *
     * @var array
     */
    protected $_fields = array();
    
    /**
     * constructor
     *
     * @param array $fields fields to match
     */
    public function __construct($fields)
    {
        $this->_fields = $fields;
    }
    
    /**
     * message
     *
     * @var string
     */
    protected $_messageTemplates = array(
        self::NOT_MATCH => 'Passwords do not match'
    );

    /**
     * validator method
     *
     * @param string $value
     * @param arrat $context
     * @return bool
     */
    public function isValid($value, $context = null)
    {
        $value = (string) $value;
        $this->_setValue($value);

        if (is_array($context)) {
            foreach($this->_fields as $field) {
                if (!isset($context[$field]) || $context[$field] != $value) {
                    $this->_error(self::NOT_MATCH);
                    return false;
                }
            }            
            return true;
        }

        $this->_error(self::NOT_MATCH);
        return false;
    }
}